package com.jumi.microservice.refund;

import com.jumi.microservice.config.WxPayConfig;
import com.jumi.microservice.dto.PayRefundRequest;
import com.jumi.microservice.utils.ClientCustomSsl;
import com.jumi.microservice.utils.WxBuildRequestUtil;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author scw
 * @date 2020/11/5
 */
public class WxPayRefund implements Serializable {
    @Resource
    WxPayConfig wxPayConfig;
    @Resource
    private ClientCustomSsl clientCustomSsl;

    public String wxPayRefundQuery(PayRefundRequest payRefundRequest) {
        SortedMap<String, Object> parameters = new TreeMap<>();
        parameters.put("appid", wxPayConfig.getAppId());
        parameters.put("mch_id", wxPayConfig.getMchId());
        parameters.put("nonce_str", WxBuildRequestUtil.getNonceStr());
        parameters.put("out_refund_no", payRefundRequest.getThirdPartyRefundSn());
        parameters.put("out_trade_no", payRefundRequest.getOrderSn());
        parameters.put("refund_id", payRefundRequest.getRefundNo());
        parameters.put("transaction_id", payRefundRequest.getThirdPartyPaySn());
        parameters.put("sign", WxBuildRequestUtil.createSign(parameters, wxPayConfig.getKey()));
        String requestXml = WxBuildRequestUtil.getRequestXml(parameters);
        String resultXml = clientCustomSsl.doPost("https://api.mch.weixin.qq.com/pay/refundquery", requestXml);
        Map<String, String> paramMap = WxBuildRequestUtil.doXMLParse(resultXml);
        //返回信息接收
        Map<String, String> returnMap = new HashMap<>();
        String params = "{" +
                "\",\"appid\":\"" + parameters.get("appid") +
                "\",\"mch_id\":\"" + parameters.get("mch_id") +
                "\",\"nonce_str\":\"" + parameters.get("nonce_str") +
                "\",\"out_refund_no\":\"" + parameters.get("out_refund_no") +
                "\",\"out_trade_no\":\"" + parameters.get("out_trade_no") +
                "\",\"refund_id\":\"" + parameters.get("refund_id") +
                "\"transaction_id\":\"" + parameters.get("transaction_id") +
                "\",\"sign\":\"" + parameters.get("sign")
                + "\"}";
        returnMap.put("parameters", params);
        returnMap.put("return_code", paramMap.get("return_code"));
        returnMap.put("return_msg", paramMap.get("return_msg"));
        returnMap.put("result_code", paramMap.get("result_code"));
        if ("SUCCESS".equals(paramMap.get("result_code"))) {
            returnMap.put("appid", paramMap.get("appid"));
            returnMap.put("mch_id", paramMap.get("mch_id"));
            returnMap.put("nonce_str", paramMap.get("nonce_str"));
            returnMap.put("sign", paramMap.get("sign"));
            returnMap.put("transaction_id", paramMap.get("transaction_id"));
            returnMap.put("out_trade_no", paramMap.get("out_trade_no"));
            returnMap.put("total_fee", paramMap.get("total_fee"));
            returnMap.put("cash_fee", paramMap.get("cash_fee"));
            returnMap.put("refund_count", paramMap.get("refund_count"));
        } else {
            returnMap.put("err_code", paramMap.get("err_code"));
            returnMap.put("err_code_des", paramMap.get("err_code_des"));
        }
        return returnMap.toString();
    }
}
